import { createRouter, createWebHistory } from 'vue-router';
import { getToken } from '@/utils/auth'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/deliveryman',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true, role: 'deliveryman' },
    children: [
      {
        path: '',
        redirect: '/deliveryman/tasks'
      },
      {
        path: 'tasks',
        name: 'DeliveryTasks',
        component: () => import('@/views/delivery/Tasks.vue'),
        meta: { title: '配送任务管理' }
      },
      {
        path: 'dashboard',
        name: 'DeliverymanDashboard',
        component: () => import('@/views/delivery/Dashboard.vue'),
        meta: { title: '配送员仪表盘' }
      },
      {
        path: 'profile',
        name: 'DeliverymanProfile',
        component: () => import('@/views/delivery/Profile.vue'),
        meta: { title: '个人信息' }
      }
    ]
  },
  {
    path: '/salesman',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true, role: 'salesman' },
    children: [
      {
        path: '',
        redirect: '/salesman/dashboard'
      },
      {
        path: 'dashboard',
        name: 'SalesmanDashboard',
        component: () => import('@/views/sales/Dashboard.vue'),
        meta: { title: '销售员仪表盘' }
      },
      {
        path: 'profile',
        name: 'SalesmanProfile',
        component: () => import('@/views/sales/Profile.vue'),
        meta: { title: '个人信息' }
      },
      {
        path: 'product-management',
        name: 'SalesmanProductManagement',
        component: () => import('@/views/sales/ProductManagement.vue'),
        meta: { title: '价格管理' }
      },
      {
        path: 'sales-report',
        name: 'SalesmanSalesReport',
        component: () => import('@/views/sales/SalesReportView.vue'),
        meta: { title: '销售报告' }
      }
    ]
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/admin',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
        meta: { title: '管理员仪表盘' }
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('@/views/admin/UserManagement.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'products',
        name: 'ProductManagement',
        component: () => import('@/views/admin/ProductManagement.vue'),
        meta: { title: '产品管理' }
      },
      {
        path: 'orders',
        name: 'OrderManagement',
        component: () => import('@/views/admin/OrderManagement.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'delivery',
        name: 'DeliveryManagement',
        component: () => import('@/views/admin/DeliveryManagement.vue'),
        meta: { title: '配送管理' }
      },
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('@/views/admin/Profile.vue'),
        meta: { title: '个人信息' }
      }
    ]
  },
  {
    path: '/user',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '主页' }
      }
    ]
  },
  {
    path: '/purchaser',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true, role: 'purchaser' },
    children: [
      {
        path: '',
        redirect: '/purchaser/dashboard'
      },
      {
        path: 'dashboard',
        name: 'PurchaserDashboard',
        component: () => import('@/views/purchaser/Dashboard.vue'),
        meta: { title: '采购员仪表盘' }
      },
      {
        path: 'profile',
        name: 'PurchaserProfile',
        component: () => import('@/views/purchaser/Profile.vue'),
        meta: { title: '个人信息' }
      },
      {
        path: 'products',
        name: 'PurchaserProducts',
        component: () => import('@/views/purchaser/Products.vue'),
        meta: { title: '产品管理' }
      },
      {
        path: 'orders',
        name: 'PurchaserOrders',
        component: () => import('@/views/purchaser/Orders.vue'),
        meta: { title: '采购订单' }
      }
    ]
  },
  {
    path: '/customer',
    component: () => import('@/layouts/CustomerLayout.vue'),
    children: [
      {
        path: '',
        redirect: '/customer/products'
      },
      {
        path: 'dashboard',
        redirect: '/customer/products'
      },
      {
        path: 'products',
        name: 'CustomerProducts',
        component: () => import('@/views/customer/Products.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/api-test',
    name: 'ApiTest',
    component: () => import('@/views/ApiTest.vue')
  },
  {
    path: '/product-api-test',
    name: 'ProductApiTest',
    component: () => import('@/views/ProductApiTest.vue')
  },
  {
    path: '/purchase-order-api-test',
    name: 'PurchaseOrderApiTest',
    component: () => import('@/views/PurchaseOrderApiTest.vue')
  },
  {
    path: '/sales-order-api-test',
    name: 'SalesOrderApiTest',
    component: () => import('@/views/SalesOrderApiTest.vue')
  },
  {
    path: '/delivery-api-test',
    name: 'DeliveryApiTest',
    component: () => import('@/views/DeliveryApiTest.vue')
  },
  {
    path: '/delivery_admin',
    component: () => import('@/layouts/UnifiedLayout.vue'),
    meta: { requiresAuth: true, role: 'delivery_admin' },
    children: [
      {
        path: '',
        redirect: '/delivery_admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'DeliveryAdminDashboard',
        component: () => import('@/views/delivery_admin/Dashboard.vue'),
        meta: { title: '配送管理员仪表盘' }
      },
      {
        path: 'tasks',
        name: 'DeliveryAdminTasks',
        component: () => import('@/views/delivery_admin/Tasks.vue'),
        meta: { title: '配送任务管理' }
      },
      {
        path: 'staff',
        name: 'DeliveryAdminStaff',
        component: () => import('@/views/delivery_admin/Staff.vue'),
        meta: { title: '配送员管理' }
      },
      {
        path: 'profile',
        name: 'DeliveryAdminProfile',
        component: () => import('@/views/delivery_admin/Profile.vue'),
        meta: { title: '个人信息' }
      }
    ]
  },
  {
    path: '/inventory-api-test', // 您可以自定义路径
    name: 'InventoryApiTest',    // 确保名称唯一
    component: () => import('@/views/InventoryApiTest.vue'), // 指向您保存的文件
    meta: { title: '库存API测试' } // 页面标题
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = getToken()
  const userStore = useUserStore()

  if (to.meta.requiresAuth) {
    if (!token) {
      next('/login')
      return
    }

    if (to.meta.role && to.meta.role !== userStore.role) {
      // 根据用户角色重定向到对应的首页
      if (userStore.role === 'deliveryman') {
        next('/deliveryman')
      } else if (userStore.role === 'salesman') {
        next('/salesman')
      } else if (userStore.role === 'delivery_admin') {
        next('/delivery_admin')
      } else {
        next('/login')
      }
      return
    }
  }

  next()
})

export default router
